package org.lwd.datalink.core.runtime.rest.resources;

import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.lwd.datalink.domain.enent.bi.RdbmsCountEvent;
import org.lwd.datalink.domain.vo.RdbmsOperatorVO;
import org.lwd.datalink.event.EventBusFactory;
import org.lwd.datalink.utils.FutureCallback;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Slf4j
@Path("/rdbms")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RdbmsOperatorResource {


    @POST
    @Path("/count")
    public String count(RdbmsOperatorVO vo) {
        log.debug("start count method");
        EventBus eventBus = EventBusFactory.getEventBus();
        RdbmsCountEvent event = new RdbmsCountEvent(new FutureCallback(), vo.getMediaSourceId(), vo.getSql());
        eventBus.post(event);
        try {
            String result = (String) event.getCallback().get();
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            log.debug("execute count method success");
        }
        return "-1";
    }

}
